package com.topone.controller;


import com.topone.Exception.FanZoneException;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dataObject.Bussiness;
import com.topone.dto.BussinessDTO;
import com.topone.service.BussinessService;
import com.topone.sms.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RequestMapping("/bussiness")
@RestController
@Slf4j
@CrossOrigin
public class LoginController {

    @Autowired
    private BussinessService bussinessService;
    @Autowired
    private SmsService smsSohuService;

    @PostMapping("/login")
    public ResultVO<Map<String, String>> bussinessLogin(@RequestParam("bussinessPhone") String bussinessPhone,
                                                        @RequestParam("bussinessPassword") String bussinessPassword,
                                                        Map<String, String> map) {

        if (StringUtils.isNotEmpty(bussinessPhone) && StringUtils.isNotEmpty(bussinessPassword)) {
            try {
                BussinessDTO bussinessDTO = bussinessService.checkBussinessPassword(bussinessPassword, bussinessPhone);
                log.info("{}", bussinessDTO);
                if (bussinessDTO != null) {
                    map.put("bussinessId", bussinessDTO.getBussinessId());
                } else {
                    return ResultVOUntil.error(1, "帐号不存在或密码不正确");
                }
            } catch (FanZoneException e) {
                log.error("【校验商家核销登录】失败", e);
                map.put("error", e.getMessage());
                return ResultVOUntil.error(1, e.getMessage());
            }
        } else {
            map.put("error", "参数为空");
            return ResultVOUntil.error(1, "参数为空");
        }
        log.info("【校验商家核销登录】成功，bussinessId={}", map.get("bussinessId"));
        return ResultVOUntil.success(map);
    }

    @GetMapping("/phoneLogin/{phone}")
    public ResultVO phoneSend(@PathVariable String phone) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(phone)) {
            Bussiness bussiness = bussinessService.findByPhone(phone);
            if (Objects.nonNull(bussiness)) {
                boolean backdoor = phone.startsWith("99");
                smsSohuService.sendSmsCode(phone, backdoor);
            } else {
                return ResultVOUntil.error(1, "商家不存在");
            }
        } else {
            return ResultVOUntil.error(1, "手机号码为空");
        }
        log.info("短信已发送:{}", phone);
        map.put("短信发送成功", phone);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/phoneCheck")
    public ResultVO phoneLogin(@RequestParam("code") String code, @RequestParam("phone") String phone) {
        Bussiness bussiness = bussinessService.findByPhone(phone);
        if (Objects.nonNull(bussiness)) {
            try {
                smsSohuService.checkSmsCode(phone, code);
            } catch (Exception e) {
                log.info("校验code失败", e.getMessage());
                return ResultVOUntil.error(1, e.getMessage());
            }
            log.info("商家登录成功:{}", bussiness.getBussinessId());
            return ResultVOUntil.success(bussiness.getBussinessId());
        } else {
            return ResultVOUntil.error(1, "商家不存在");
        }
    }


}
